package com.threadpool.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.*;

@RestController
@RequestMapping("threadpool")
public class ThreadPoolController {

    private static BlockingQueue blockingQueue = new ArrayBlockingQueue<>(20);
    private static ExecutorService executorService = new ThreadPoolExecutor(500,500
            , 1, TimeUnit.DAYS, blockingQueue);

    /**
     * 启动线程测试
     * @return
     */
    @GetMapping("startPool")
    public String startPool() {
        for (int i = 0; i < 500; i++) {
            int finalI = i;
            executorService.execute(() -> {
                int index = 1;
                while (true) {
                    index += 1;
                    System.out.println(Thread.currentThread().getName() + "任务：" + finalI + ",size: " + index);
                }
            });
        }
        return "";
    }

    @GetMapping("getPoolTest")
    public String getPoolTest() {
        return blockingQueue.size() + "";
    }
}
